<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千峰教育大前端Vue教学</title>
  <!-- 引入vue -->
  <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app">
    <!-- 声明模板内容 -->
    <div>
      <div>还有{{leftTime}}分钟下课</div>
      <div>{{state}}</div>
    </div>
  </div>
  <script>
    const { createApp, computed, watch, ref } = Vue;
    const app = createApp({
      setup() {
        const time = ref(0); // 上课时间
        const state = ref('上课中...'); // 状态
        const leftTime = computed(() => 5 - time.value); // 上课剩余时间
        // 监听上课剩余时间
        watch(leftTime, (cur, pre) => {
          if (cur === 0) {
            state.value = '下课了';
            clearInterval(timerId);
          }
        });
        let timerId;
        const updateTime = () => { // 更新上课时间
          timerId = setInterval(()=>{ time.value++; }, 1000); // 每秒上课时间+1
        }
        return { // 返回值会暴露给模板，模板可以直接使用
          time,
          state,
          leftTime,
          updateTime,
        };
      },
      created() {
        this.updateTime(); // 调用setup暴露出来的函数
      },
    });
    // 挂载应用
    app.mount('#app');
  </script>
</body>

</html>